import * as pathAnim from './pathAnim.js'
export default {
  data () {
    return {
      // oMap: null, // 地图对象;    简写对照 overlay[ovl], arrow[arw], sprite[spt], Endpoint[ept]
      animaID: null, // 动画ID-- 用来保存当前动画id
      pathSpritesLs: [], // 路径精灵列表-保存每个精灵
      basePath: null, // 当前动画用路径
      animLayer: null // ol对象-当前动画层
    }
  },
  watch: {
    // watch
  },
  mounted () {
  },
  methods: {
    // -- 路径动画IO部分(提供给cardMap使用)
    /**
     * @description 创建动画路径
     * @param {string} id 路径的ID
     * @param {array} aPointList 组成路径的的点
     * @param {boolean} isDisplayLen 是否显示路径长度
     * @param {string} layerName 指定图层名称，用于绘制路径
     * @return {object} {创建的路径geom和长度}
     */
    createPath (id, aPointList, isDisplayLen = false, layerName = 'poiLayer', fLenTxtPos = 0.5) {
      // const oMapLayer = this.getLayerByName(layerName, this.imap)
      this.animLayer = this.getLayerByName(layerName, this.imap)
      const hasPath = pathAnim.hasPathByID(id, this.animLayer)
      if (hasPath) {
        // console.log('路径存在--不再绘制!')
        // pathAnim.getPathByID(id, oMapLayer)
        // pathAnim.delAllPath(oMapLayer) // -- 删除动画路径
        // this.delPathByID(id, layerName) // -- 删除指定ID的路径
        // pathAnim.delAllPath(oMapLayer)
        // const myClass = new pathAnim.PathSprite('212', 'plane', 2200, 882.0576) // 1 马赫=1225.08 千米/时; V型“战斧”速度达到0.72马赫，最大射程2400公里！
        // myClass.createSprite(this.basePath, oMapLayer)
        // this.pathSpritesLs.push(myClass)
        // console.log(this.pathSpritesLs)
      } else {
        // 绘制路径
        const res = pathAnim.createPath(id, aPointList, isDisplayLen, this.animLayer, this.conf.RCS, fLenTxtPos)
        this.basePath = res.route
        const fLen = res.pathLen
        // console.log(this.basePath, fLen)
        const data = { id: id, distance: fLen }
        this.$emit('pathLen', data)
      }
    },

    createTwoPointPath (id, startPoint, endPoint, isDisplayLen = false, layerName = 'poiLayer', fLenTxtPos = 0.5) {
      const aPointList = [startPoint, endPoint]
      this.createPath(id, aPointList, isDisplayLen, layerName, fLenTxtPos)
    },

    createSinglePath (id, startPoint, endPoint, isDisplayLen = false, layerName = 'poiLayer', fLenTxtPos = 0.5) {
      this.delAllSprite()
      this.delAllPath()
      const aPointList = [startPoint, endPoint]
      this.createPath(id, aPointList, isDisplayLen, layerName, fLenTxtPos)
    },

    /**
    * @description 删除指定ID的路径
    * @param {string} id 路径的ID
    * @param {string} layerName 指定图层名称，用于绘制路径--删除--根成全局变量
    * @return null
    */
    delPathByID (id) {
      // const oMapLayer = this.getLayerByName(layerName, this.imap)
      if (this.animLayer) {
        const res = pathAnim.delPathByID(id, this.animLayer)
        if (res) {
          this.basePath = null
          const numLen = this.pathSpritesLs.length
          for (let i = 0; i < numLen; i++) {
            const iClass = this.pathSpritesLs[i]
            if (id === iClass.id) {
              this.pathSpritesLs.splice(i, 1)
            }
          }
        }
      }
    },

    /**
    * @description 删除所有路径
    * @return null
    */
    delAllPath () {
      // const oMapLayer = this.getLayerByName(layerName, this.imap)
      if (this.animLayer) {
        pathAnim.delAllPath(this.animLayer)
        // 删除当前的route数据
        this.basePath = null
        this.pathSpritesLs.splice(0, this.pathSpritesLs.length)
      }
    },

    /**
    * @description 删除指定ID的精灵
    * @param {string} id 路径的ID
    * @param {string} layerName 指定图层名称，用于绘制路径
    * @return null
    */
    delSpriteByID (id) {
      if (this.animLayer) {
        const mapLayerSource = this.animLayer.getSource()
        if (!mapLayerSource) return // 指定的id路线存在
        const iFeature = mapLayerSource.getFeatureById('PATH_SPT' + id)
        if (iFeature) {
          mapLayerSource.removeFeature(iFeature)
        }
      }
      const numSprite = this.pathSpritesLs.length
      for (let i = 0; i < numSprite; i++) {
        const iSprite = this.pathSpritesLs[i]
        if (iSprite.id === id) {
          this.pathSpritesLs.splice(i, 1)
          break
        }
      }
    },

    /**
    * @description 删除所有的路径的精灵
    * @param {string} id 路径的ID
    * @param {string} layerName 指定图层名称，用于绘制路径
    * @return null
    */
    delAllSprite () {
      if (this.animLayer) {
        const mapLayerSource = this.animLayer.getSource()
        if (!mapLayerSource) return // 指定的id路线存在
        const allFeatures = mapLayerSource.getFeatures()
        const arrLen = allFeatures.length
        for (let i = arrLen - 1; i >= 0; i--) {
          const iFeature = allFeatures[i]
          const sID = iFeature.getId()
          if (sID.substr(0, 8) === 'PATH_SPT') {
            mapLayerSource.removeFeature(iFeature)
          }
        }
      }
      const numSprite = this.pathSpritesLs.length
      if (numSprite > 0) {
        this.pathSpritesLs.splice(0, numSprite)
      }
    },

    /**
    * @description 获取指定ID的精灵
    * @param {string} id 路径的ID
    * @return null
    */
    getSpriteByID (id) {
      if (this.animLayer) {
        const mapLayerSource = this.animLayer.getSource()
        if (!mapLayerSource) return // 指定的id路线存在
        return mapLayerSource.getFeatureById('PATH_SPT' + id)
      }
    },

    // 动画循环
    animaLoop () {
      const numSprite = this.pathSpritesLs.length
      for (let i = 0; i < numSprite; i++) {
        const iSprite = this.pathSpritesLs[i]
        if (iSprite.animating) {
          const data = iSprite.moveFeature(this.animLayer) // render()
          if (data) { // 如果时间不足1秒不会有返回数据
            // console.log(this.getTwoPointDistance([113.542241, 22.386853], data.coord))
            this.$emit('pathAnim', data)
          }
        }
      }
    },
    // 开始动画 创建动画精灵 设置动画开始标志和时间 -- 如果循环为开始就可以显示动画了
    startPathAnima (id, icon, distance = 100, speed = 1) {
      // const oMapLayer = this.getLayerByName(layerName, this.imap)
      // 如果指定精灵已存在退出
      const hasFeature = this.getSpriteByID(id)
      if (hasFeature) return
      // const hasPath = pathAnim.hasPathByID(id, oMapLayer) // 查看有没有路径
      const animRoute = pathAnim.getPathByID(id, this.animLayer) // 获取动画路径
      // 没有关联动画路径，需要先创建路线
      if (!animRoute) return
      const myClass = new pathAnim.PathSprite(id, icon, distance, speed) // 1 马赫=1225.08 千米/时; V型“战斧”速度达到0.72马赫，最大射程2400公里！
      // myClass.createSprite(this.basePath, oMapLayer) // 创建动画精灵
      myClass.createSprite(animRoute, this.animLayer)
      myClass.startAnimation() // 精灵开始标志和时间设置
      this.pathSpritesLs.push(myClass)
      console.log(this.pathSpritesLs)
    },
    // 动画停止后重新开始
    restartAnimaLoop () {
      const numSprite = this.pathSpritesLs.length
      for (let i = 0; i < numSprite; i++) {
        const iSprite = this.pathSpritesLs[i]
        if (iSprite.animating) {
          // iSprite.lastTime设置为当前时间
          iSprite.setLastTime()
        }
      }
      this.animaID = requestAnimationFrame(this.startAnimaLoop)
    },
    // 整体动画循环设置--后期可能会添加很多
    startAnimaLoop () {
      this.animaLoop()
      this.animaID = requestAnimationFrame(this.startAnimaLoop)
    },
    stopAnimaLoop () {
      cancelAnimationFrame(this.animaID)
    },

    // 根据精灵id从pathSpritesLs获取精灵
    getPathSpriteByID (id) {
      const numSprite = this.pathSpritesLs.length
      for (let i = 0; i < numSprite; i++) {
        const iSprite = this.pathSpritesLs[i]
        if (iSprite.getID() === id) {
          return iSprite
        }
      }
      return null
    },

    // 极端到达目标的时间(返回单位--秒)
    getDistanceTime (spriteID, coord1, coord2, sRCS1 = 'EPSG:4326', sRCS2 = 'EPSG:3857') {
      const distance = this.getTwoPointDistance(coord1, coord2, sRCS1 = 'EPSG:4326', sRCS2 = 'EPSG:3857')
      const iSpeed = this.getPathSpriteByID(spriteID).getSpeed()
      if (iSpeed) {
        return { len: distance, time: distance / iSpeed }
      } else {
        return { len: distance, time: 0 }
      }
    }
  }
}
